package com.lebang.controller.sys;

import com.alibaba.fastjson.JSONObject;
import com.lebang.aop.ConfigRecord;
import com.lebang.common.Login;
import com.lebang.domain.AppAdControAppStore;
import com.lebang.domain.AppRousePlan;
import com.lebang.domain.TaskRouse;
import com.lebang.entity.sys.Module;
import com.lebang.example.AppAdControAppStoreExample;
import com.lebang.example.AppRousePlanExample;
import com.lebang.service.AppAdControService;
import com.lebang.service.NewRouseService;
import com.lebang.service.earn.AppRousePlanService;
import com.lebang.service.sys.AppAdControAppStoreService;
import com.lebangweb.response.Callback;
import com.lebangweb.response.JsonData;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;


/**
 * 渠道控制
 *
 * @author hwq
 * @date 2020-05-19
 */
@Controller
@RequestMapping("/sys/appadcontroappstore")
public class AppAdControAppStoreController {
    @Autowired
    private AppAdControAppStoreService baseService;
    @Autowired
    private NewRouseService newRouseService;
    @Autowired
    private AppRousePlanService appRousePlanService;
    @Autowired
    private AppAdControService appAdControService;
    @GetMapping("/list")
    public String list(String adKey,Model model) {
        model.addAttribute("adKey",adKey);
        return "/appadcontroappstore/list";
    }

    @GetMapping("/add")
    public String add(Integer id, Model model,String adKey) {
        AppAdControAppStore vo;
        if (id != null) {
            vo = baseService.selectById(id);
        } else {
            vo = new AppAdControAppStore();
            vo.setAdKey(adKey);
            vo.setSdkFirstKey("CSJ");
            vo.setSdkLastKey("CSJ");
            vo.setAdKey(adKey);
        }
        List<TaskRouse> rouses = newRouseService.selectAll();
        List<AppRousePlan> list=appRousePlanService.selectByExample(new AppRousePlanExample());
        model.addAttribute("list", rouses);
        model.addAttribute("item", vo);
        model.addAttribute("app", list);
        return "/appadcontroappstore/edit";
    }


    @PostMapping("/edit")
    @ResponseBody
    @ConfigRecord
    public JsonData edit(AppAdControAppStore vo) {
        if (vo.getId() != null) {
            vo.setModifyTime(LocalDateTime.now());
            baseService.updateById(vo);
        } else {
            vo.setCreateTime(LocalDateTime.now());
            vo.setHasDeleted(0);
            baseService.insertModel(vo);
        }
        return Callback.success("success", "操作成功");
    }

    @Login
    @PostMapping("/enable")
    @ResponseBody
    @ConfigRecord
    public JsonData enable(@RequestBody AppAdControAppStore vo) {
        baseService.updateById(vo);
        return Callback.success("success", "操作成功");
    }

    @Login
    @PostMapping("/delete")
    @ResponseBody
    public JsonData delete(Integer id) {
        baseService.deleteById(id);
        return Callback.success("success", "操作成功");
    }

    @ApiOperation(value = "分页")
    @PostMapping("/list")
    @ResponseBody
    public JSONObject list(HttpServletRequest request, @RequestParam Integer page, @RequestParam Integer limit
    ,String adKey) {
        AppAdControAppStoreExample example = new AppAdControAppStoreExample();
        example.setOffset((page - 1) * limit.longValue());
        example.setLimit(limit);
        AppAdControAppStoreExample.Criteria criteria = example.createCriteria();
        criteria.andAdKeyEqualTo(adKey);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 0);
        List<AppAdControAppStore> data = baseService.selectByExample(example);
        data.forEach(s->{
            s.setTodayCount(appAdControService.getCountByKey(s.getAdKey(),s.getAppStore()));
            s.setYesterDayCount(appAdControService.getCountByKeyYesterday(s.getAdKey(),s.getAppStore()));
        });
        jsonObject.put("data", data);
        jsonObject.put("count", baseService.countByExample(example));
        return jsonObject;
    }
}
